<?php

/**
 * sfGuardUser form base class.
 *
 * @method sfGuardUser getObject() Returns the current form's model object
 *
 * @package    sfwixet
 * @subpackage form
 * @author     Your name here
 * @version    SVN: $Id: sfDoctrineFormGeneratedTemplate.php 29553 2010-05-20 14:33:00Z Kris.Wallsmith $
 */
abstract class BasesfGuardUserForm extends BaseFormDoctrine
{
  public function setup()
  {
    $this->setWidgets(array(
      'id'                                      => new sfWidgetFormInputHidden(),
      'username'                                => new sfWidgetFormInputText(),
      'firstName'                               => new sfWidgetFormInputText(),
      'lastName'                                => new sfWidgetFormInputText(),
      'secondLastName'                          => new sfWidgetFormInputText(),
      'email_address'                           => new sfWidgetFormInputText(),
      'algorithm'                               => new sfWidgetFormInputText(),
      'salt'                                    => new sfWidgetFormInputText(),
      'password'                                => new sfWidgetFormInputText(),
      'language_id'                             => new sfWidgetFormDoctrineChoice(array('model' => $this->getRelatedModelName('language'), 'add_empty' => true)),
      'theme_id'                                => new sfWidgetFormDoctrineChoice(array('model' => $this->getRelatedModelName('theme'), 'add_empty' => true)),
      'is_active'                               => new sfWidgetFormInputCheckbox(),
      'is_super_admin'                          => new sfWidgetFormInputCheckbox(),
      'last_login'                              => new sfWidgetFormDateTime(),
      'main_album_id'                           => new sfWidgetFormDoctrineChoice(array('model' => $this->getRelatedModelName('mainAlbum'), 'add_empty' => true)),
      'main_photo_id'                           => new sfWidgetFormDoctrineChoice(array('model' => $this->getRelatedModelName('face'), 'add_empty' => true)),
      'main_group_id'                           => new sfWidgetFormDoctrineChoice(array('model' => $this->getRelatedModelName('mainGroup'), 'add_empty' => true)),
      'main_private_messages_directory_id'      => new sfWidgetFormDoctrineChoice(array('model' => $this->getRelatedModelName('mainPrivateMessagesDirectory'), 'add_empty' => true)),
      'main_private_messages_sent_directory_id' => new sfWidgetFormDoctrineChoice(array('model' => $this->getRelatedModelName('mainPrivateMessagesSentDirectory'), 'add_empty' => true)),
      'last_activity'                           => new sfWidgetFormInputText(),
      'font_id'                                 => new sfWidgetFormDoctrineChoice(array('model' => $this->getRelatedModelName('font'), 'add_empty' => true)),
      'text_color'                              => new sfWidgetFormInputText(),
      'created_at'                              => new sfWidgetFormDateTime(),
      'updated_at'                              => new sfWidgetFormDateTime(),
      'groups_list'                             => new sfWidgetFormDoctrineChoice(array('multiple' => true, 'model' => 'sfGuardGroup')),
      'acl_groups_list'                         => new sfWidgetFormDoctrineChoice(array('multiple' => true, 'model' => 'aclGroup')),
      'permissions_list'                        => new sfWidgetFormDoctrineChoice(array('multiple' => true, 'model' => 'sfGuardPermission')),
    ));

    $this->setValidators(array(
      'id'                                      => new sfValidatorChoice(array('choices' => array($this->getObject()->get('id')), 'empty_value' => $this->getObject()->get('id'), 'required' => false)),
      'username'                                => new sfValidatorString(array('max_length' => 128)),
      'firstName'                               => new sfValidatorString(array('max_length' => 128)),
      'lastName'                                => new sfValidatorString(array('max_length' => 128)),
      'secondLastName'                          => new sfValidatorString(array('max_length' => 128)),
      'email_address'                           => new sfValidatorString(array('max_length' => 128)),
      'algorithm'                               => new sfValidatorString(array('max_length' => 128, 'required' => false)),
      'salt'                                    => new sfValidatorString(array('max_length' => 128, 'required' => false)),
      'password'                                => new sfValidatorString(array('max_length' => 128, 'required' => false)),
      'language_id'                             => new sfValidatorDoctrineChoice(array('model' => $this->getRelatedModelName('language'), 'required' => false)),
      'theme_id'                                => new sfValidatorDoctrineChoice(array('model' => $this->getRelatedModelName('theme'), 'required' => false)),
      'is_active'                               => new sfValidatorBoolean(array('required' => false)),
      'is_super_admin'                          => new sfValidatorBoolean(array('required' => false)),
      'last_login'                              => new sfValidatorDateTime(array('required' => false)),
      'main_album_id'                           => new sfValidatorDoctrineChoice(array('model' => $this->getRelatedModelName('mainAlbum'), 'required' => false)),
      'main_photo_id'                           => new sfValidatorDoctrineChoice(array('model' => $this->getRelatedModelName('face'), 'required' => false)),
      'main_group_id'                           => new sfValidatorDoctrineChoice(array('model' => $this->getRelatedModelName('mainGroup'), 'required' => false)),
      'main_private_messages_directory_id'      => new sfValidatorDoctrineChoice(array('model' => $this->getRelatedModelName('mainPrivateMessagesDirectory'), 'required' => false)),
      'main_private_messages_sent_directory_id' => new sfValidatorDoctrineChoice(array('model' => $this->getRelatedModelName('mainPrivateMessagesSentDirectory'), 'required' => false)),
      'last_activity'                           => new sfValidatorPass(array('required' => false)),
      'font_id'                                 => new sfValidatorDoctrineChoice(array('model' => $this->getRelatedModelName('font'), 'required' => false)),
      'text_color'                              => new sfValidatorPass(array('required' => false)),
      'created_at'                              => new sfValidatorDateTime(),
      'updated_at'                              => new sfValidatorDateTime(),
      'groups_list'                             => new sfValidatorDoctrineChoice(array('multiple' => true, 'model' => 'sfGuardGroup', 'required' => false)),
      'acl_groups_list'                         => new sfValidatorDoctrineChoice(array('multiple' => true, 'model' => 'aclGroup', 'required' => false)),
      'permissions_list'                        => new sfValidatorDoctrineChoice(array('multiple' => true, 'model' => 'sfGuardPermission', 'required' => false)),
    ));

    $this->validatorSchema->setPostValidator(
      new sfValidatorAnd(array(
        new sfValidatorDoctrineUnique(array('model' => 'sfGuardUser', 'column' => array('username'))),
        new sfValidatorDoctrineUnique(array('model' => 'sfGuardUser', 'column' => array('email_address'))),
      ))
    );

    $this->widgetSchema->setNameFormat('sf_guard_user[%s]');

    $this->errorSchema = new sfValidatorErrorSchema($this->validatorSchema);

    $this->setupInheritance();

    parent::setup();
  }

  public function getModelName()
  {
    return 'sfGuardUser';
  }

  public function updateDefaultsFromObject()
  {
    parent::updateDefaultsFromObject();

    if (isset($this->widgetSchema['groups_list']))
    {
      $this->setDefault('groups_list', $this->object->groups->getPrimaryKeys());
    }

    if (isset($this->widgetSchema['acl_groups_list']))
    {
      $this->setDefault('acl_groups_list', $this->object->aclGroups->getPrimaryKeys());
    }

    if (isset($this->widgetSchema['permissions_list']))
    {
      $this->setDefault('permissions_list', $this->object->permissions->getPrimaryKeys());
    }

  }

  protected function doSave($con = null)
  {
    $this->savegroupsList($con);
    $this->saveaclGroupsList($con);
    $this->savepermissionsList($con);

    parent::doSave($con);
  }

  public function savegroupsList($con = null)
  {
    if (!$this->isValid())
    {
      throw $this->getErrorSchema();
    }

    if (!isset($this->widgetSchema['groups_list']))
    {
      // somebody has unset this widget
      return;
    }

    if (null === $con)
    {
      $con = $this->getConnection();
    }

    $existing = $this->object->groups->getPrimaryKeys();
    $values = $this->getValue('groups_list');
    if (!is_array($values))
    {
      $values = array();
    }

    $unlink = array_diff($existing, $values);
    if (count($unlink))
    {
      $this->object->unlink('groups', array_values($unlink));
    }

    $link = array_diff($values, $existing);
    if (count($link))
    {
      $this->object->link('groups', array_values($link));
    }
  }

  public function saveaclGroupsList($con = null)
  {
    if (!$this->isValid())
    {
      throw $this->getErrorSchema();
    }

    if (!isset($this->widgetSchema['acl_groups_list']))
    {
      // somebody has unset this widget
      return;
    }

    if (null === $con)
    {
      $con = $this->getConnection();
    }

    $existing = $this->object->aclGroups->getPrimaryKeys();
    $values = $this->getValue('acl_groups_list');
    if (!is_array($values))
    {
      $values = array();
    }

    $unlink = array_diff($existing, $values);
    if (count($unlink))
    {
      $this->object->unlink('aclGroups', array_values($unlink));
    }

    $link = array_diff($values, $existing);
    if (count($link))
    {
      $this->object->link('aclGroups', array_values($link));
    }
  }

  public function savepermissionsList($con = null)
  {
    if (!$this->isValid())
    {
      throw $this->getErrorSchema();
    }

    if (!isset($this->widgetSchema['permissions_list']))
    {
      // somebody has unset this widget
      return;
    }

    if (null === $con)
    {
      $con = $this->getConnection();
    }

    $existing = $this->object->permissions->getPrimaryKeys();
    $values = $this->getValue('permissions_list');
    if (!is_array($values))
    {
      $values = array();
    }

    $unlink = array_diff($existing, $values);
    if (count($unlink))
    {
      $this->object->unlink('permissions', array_values($unlink));
    }

    $link = array_diff($values, $existing);
    if (count($link))
    {
      $this->object->link('permissions', array_values($link));
    }
  }

}
